﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.Common;
using System.Data;
using System.Configuration;

using Microsoft.Practices.EnterpriseLibrary.Data;

namespace Unis.ORM.Persistence
{
    public class DbCommandExecuteTrack
    {
        // Fields
        private static DbCommandExecuteTrack _Instance;
        private static object _Obj = new object();
        private static readonly string RUN_INFO_RECORD_DB_NAME = "RunInfoRecordDbName";

        // Methods
        protected DbCommandExecuteTrack()
        {
        }

        public string CommandToTrackMessage(Database db, DbCommand dbCmd)
        {
            string str = " CommandText:";
            if ((db != null) && !string.IsNullOrEmpty(db.ConnectionStringWithoutCredentials))
            {
                string str2 = ConfigurationManager.AppSettings[RUN_INFO_RECORD_DB_NAME];
                if (!string.IsNullOrEmpty(str2) && (string.Compare(str2, "True", true) == 0))
                {
                    str = db.ConnectionStringWithoutCredentials.Split(new char[] { ';' })[0] + str;
                }
            }
            return (str + dbCmd.CommandText + this.toCommandParameters(dbCmd));
        }

        private string toCommandParameters(DbCommand dbCmd)
        {
            if ((dbCmd.Parameters == null) || (dbCmd.Parameters.Count == 0))
            {
                return "(没有参数)";
            }
            string str = "(参数:";
            foreach (DbParameter parameter in dbCmd.Parameters)
            {
                str = str + parameter.ParameterName + "=>";
                if (parameter.Value == null)
                {
                    str = str + "NULL";
                }
                else
                {
                    str = str + "'" + parameter.Value.ToString() + "'";
                }
                if (parameter.Direction != ParameterDirection.Input)
                {
                    str = str + " " + parameter.Direction.ToString();
                }
                str = str + ",";
            }
            return (str.Remove(str.Length - 1, 1) + ")");
        }

        private string toCommandSqlString(DbCommand dbCmd)
        {
            if (dbCmd.Parameters.Count == 0)
            {
                return dbCmd.CommandText;
            }
            string commandText = dbCmd.CommandText;
            foreach (DbParameter parameter in dbCmd.Parameters)
            {
                string newValue = (parameter.Value == null) ? "NULL" : ("'" + parameter.Value.ToString() + "'");
                commandText = commandText.Replace(":" + parameter.ParameterName, newValue);
            }
            return commandText;
        }

        // Properties
        public static DbCommandExecuteTrack Instance
        {
            get
            {
                if (_Instance == null)
                {
                    lock (_Obj)
                    {
                        if (_Instance == null)
                        {
                            _Instance = new DbCommandExecuteTrack();
                        }
                    }
                }
                return _Instance;
            }
        }
    }
}
